package sample.pages;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.web.controls.Page;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 11-5-4
 * Time: 下午4:25
 */
public class ChartPage extends Page {
    protected List<Map<String, Object>> data = new ArrayList<Map<String, Object>> ();
    protected List<Map<String, Object>> lineData = new ArrayList<Map<String, Object>> ();

    public ChartPage () {
        for (int i = 0; i < 5; i ++) {
            String name = "Product " + (i + 1);
            int value = (int) (Math.random () * 10000 + 10000);
            Map<String, Object> item = new HashMap<String, Object> ();
            item.put ("name", name);
            item.put ("value", value);
            data.add (item);
        }

        Calendar c = Calendar.getInstance ();
        c.set (Calendar.MONTH, 0);
        c.set (Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy.MM");
        for (int i = 0; i < 12; i ++) {
            c.set (Calendar.MONTH, i);
            String name = sdf.format (c.getTime ());
            int value = (int) (Math.random () * 1000 + 900);
            Map<String, Object> item = new HashMap<String, Object> ();
            item.put ("category", "sales");
            item.put ("name", name);
            item.put ("value", value);
            lineData.add (item);

            value = (int) (Math.random () * 1000 + 900);
            item = new HashMap<String, Object> ();
            item.put ("category", "cash");
            item.put ("name", name);
            item.put ("value", value);
            lineData.add (item);
        }
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        try {
            dataBind ();
        } catch (Throwable throwable) {
            throwable.printStackTrace ();
            throw new EventException (throwable);
        }
    }
}